package com.wythe.mall.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.activity.MainActivity;
import com.wythe.mall.activity.SearchActivity;
import com.wythe.mall.adapter.DataModel;
import com.wythe.mall.adapter.ShopAdapter;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ClassifyBean;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class CategoryFragment extends Fragment implements View.OnClickListener {

    private LayoutInflater inflater;
    private ViewPager viewPager;
    private ScrollView scrollView;
    private View view;
    private List<String> list;
    private ShopAdapter shopAdapter;
    private View[] views;
    private TextView[] textList;
    private List<ClassifyBean> mBeanList;
    private List<ClassifyBean.GoodsVoListBean> listType;
    // 默认的ViewPager选中的项
    private int currentItem = 0;
    private int currentPosition = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_category, container, false);
        initView();
        return view;
    }

    private void initView() {
        ((TextView) view.findViewById(R.id.titleText)).setText("分类");
        view.findViewById(R.id.title_left_button).setVisibility(View.INVISIBLE);
        view.findViewById(R.id.search_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GotoActivity.gotoActiviy(getActivity(),SearchActivity.class);
            }
        });
        scrollView = (ScrollView) view.findViewById(R.id.tools_scrollView);
        viewPager = (ViewPager) view.findViewById(R.id.goods);
        inflater = LayoutInflater.from(getActivity());
    }

    private void initData() {
        JSONObject param = new JSONObject();

        ApiOkHttp.getClassifyShop(getActivity(), param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (mBeanList == null) {
                    mBeanList = new ArrayList<>();
                }
                mBeanList.clear();
                JSONArray array = null;
                try {
                    array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ClassifyBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ClassifyBean.class);
                        mBeanList.add(bean);
                    }
                    showTools();
                    initViewPager();

                    if (viewPager.getCurrentItem() != currentPosition) {
                        viewPager.setCurrentItem(currentPosition);
                    }
                    // 通过ViewPager监听点击字体颜色和背景的改变
                    if (currentItem == currentPosition) {
                        changeTextColor(currentPosition);
                        changeTextLocation(currentPosition);
                    }
                    currentItem = currentPosition;
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    /**
     * 左侧栏目选项的内容的显示
     */
    private void showTools() {
        // TODO Auto-generated method stub
        list.clear();
        if (mBeanList != null) {
            for (int i = 0; i < mBeanList.size(); i++) {
                list.add(mBeanList.get(i).getTitle());
            }
        }
        views = new View[list.size()];
        textList = new TextView[list.size()];
        LinearLayout toolsLayout = (LinearLayout) view.findViewById(R.id.tools);

        for (int i = 0; i < list.size(); i++) {
            View view = inflater.inflate(R.layout.toolslist_layout, null);
            // 给每个View设定唯一标识
            view.setId(i);
            // 给每个view添加点击监控事件
            view.setOnClickListener(toolsItemClickListener);
            // 获取到左侧栏的的TextView的组件
            TextView textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(list.get(i));
            toolsLayout.addView(view);
            // 传入的是地址不是复制的值
            textList[i] = textView;
            views[i] = view;
        }
        // 开始默认是第一个被选中的情况
        changeTextColor(0);

    }

    /**
     * 右侧栏目选项的内容的显示
     */
    private void initViewPager() {
        // TODO Auto-generated method stub
        // 由于使用了支持包所以最终必须确保所有的导入包都是来自支持包
        shopAdapter = new ShopAdapter(getActivity().getSupportFragmentManager(), mBeanList);
        viewPager.setAdapter(shopAdapter);
//        viewPager.setOffscreenPageLimit(mBeanList.size());
        //为ViewPager设置页面变化的监控
        viewPager.addOnPageChangeListener(onPageChangeListener);
    }

    /**
     * ViewPager监控事件处理 监听ViewPager选项卡变化事的事件
     */
    private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            if (viewPager.getCurrentItem() != arg0) {
                viewPager.setCurrentItem(arg0);
            }
            // 通过ViewPager监听点击字体颜色和背景的改变
            if (currentItem != arg0) {
                changeTextColor(arg0);
                changeTextLocation(arg0);
            }
            currentItem = arg0;

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    };

    /**
     * 模拟点击时TextView文本字体颜色的改变情况
     *
     * @param id
     */
    private void changeTextColor(int id) {
        // TODO Auto-generated method stub
        for (int i = 0; i < list.size(); i++) {
            if (id != i) {
                textList[i].setBackgroundColor(0x00000000);
                textList[i].setTextColor(0xFF000000);
            }
        }
        textList[id].setBackgroundColor(0xFFFFFFFF);
        textList[id].setTextColor(0xFFFF5D5E);

    }

    /**
     * view的点击事件,
     * <p>
     * 通过点击view来是ViewPager的状态发生改变，同时ViewPager监控自己的改变 可以用来处理一些事件的情况
     */
    private View.OnClickListener toolsItemClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            viewPager.setCurrentItem(v.getId());

        }
    };

    private void addFragmentToStack(Fragment fragment) {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        //        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
        ft.replace(R.id.goods, fragment);
        ft.commit();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        currentPosition = ((MainActivity) getActivity()).getmLabel();
        if (list != null && list.size() > 0) {
            if (viewPager.getCurrentItem() != currentPosition) {
                viewPager.setCurrentItem(currentPosition);
            }
            // 通过ViewPager监听点击字体颜色和背景的改变
            if (currentItem == currentPosition) {
                changeTextColor(currentPosition);
                changeTextLocation(currentPosition);
            }
            currentItem = currentPosition;
        } else {
            initData();
        }
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 改变左侧栏选中项的位置
     *
     * @param clickPosition
     */
    private void changeTextLocation(int clickPosition) {
        // TODO Auto-generated method stub
        // 获得点击的view视图距离屏幕顶部的距离
        int y = (views[clickPosition].getTop());
        // 如果滑动条可以滑动的情况下就把点击的视图移动到顶部
        scrollView.smoothScrollTo(0, y);

    }
}
